# -*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider
from scrapy.xlib.pydispatch import dispatcher
from scrapy.http import FormRequest, Request
from scrapy import signals, log
from bs4 import BeautifulSoup
#from porsche.items import PorscheItem
import json
from database import Database, Mobile
from datetime import datetime
import time


class mobileRegistered(CrawlSpider):

	name = 'mr'
	base_url = 'http://www.weibo.com/signup/v5/formcheck?type=mobilesea&zone=0086&value=%s&from=&__rnd=1444274106406'

	# COOKIES_ENABLED=False

	# def check_ip(self, response):
	# 	pub_ip = response.xpath('//body/text()').re('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')[0]
	# 	print "My public IP is: " + pub_ip
    #
	# def start_requests(self):
	# 	req = Request('http://checkip.dyndns.org/', callback=self.check_ip)
	# 	yield req

	def start_requests(self):
		Database().connect()
		with Database.dbo.session.begin():
			results = Database.dbo.session.query(Mobile).filter(Mobile.exist == '0').all()
		for mobile in results:
			mb = str(mobile.num)
			yield Request(self.base_url%mb , headers={'Host':'www.weibo.com',
				'Referer':'http://www.weibo.com/signup/mobile.php?lang=en-us&inviteCode=&from=&appsrc=&backurl=&showlogo=',
				'Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36'}
				, callback=self.parse_exist)

	def parse_exist(self, response):
		mobile = response.url[response.url.index('&value=')+7:]
		mobile = mobile[0: mobile.index('&')]
		re = json.loads(response.body_as_unicode())
		msg = re['data']['msg']
		code = re['code']
		exist = '0'
		if 'btn_login' in msg:
			exist = '1'

		with Database.dbo.session.begin():
			Database.dbo.session.merge(Mobile({'num':mobile, 'exist':exist, 'timestamp':datetime.now()}))

		if code == '600001' and 'btn_login' not in msg:
			print 'msg: %s' % msg
			print 'sleep 5 mins...'
			time.sleep(5*60)




# # -*- coding: utf-8 -*-
# from scrapy.contrib.spiders import CrawlSpider
# import scrapy
# # from scrapy.xlib.pydispatch import dispatcher
# from scrapy.http import FormRequest, Request
# # from scrapy import signals, log
# # from bs4 import BeautifulSoup
# # from porsche.items import PorscheItem
# import json
# import math
# import re
# import urllib
# import codecs
# import time
# from Configuration import Config
# import random
#
# class mobileRegistered(CrawlSpider):
#
# 	name = 'mr'
# 	download_delay = 2
# 	base_url = 'http://www.weibo.com/signup/v5/formcheck?type=mobilesea&zone=0086&value=%s&from=&__rnd=1444274106406'
# 	COOKIES_ENABLED=False
# 	mobile = ['134','135','136','137','138','139','147','150','151','152','157','158','159','178','182','183','184','187','188',
# 	'130','131','132','145','155','156','176','185','186',
# 	'133','153','177','180','181','189',
# 	'170','171']
#
# 	outpath = 'mobileCheck.csv'
# 	def start_requests(self):
# 		for mobilePrefix in self.mobile:
# 			min = int(mobilePrefix + '')
# 			max = int(mobilePrefix + "")
# 			for mobile in xrange(0, 99999999):
# 				agent = random.choice(Config.USER_AGENTS)
# 				mobile = '%s%s'% (mobilePrefix, '{0:08}'.format(mobile))
# 				#print mobile
# 				print '-------------------------------------------------'
# 				print '-------------------------------------------------'
# 				print agent
# 				req = Request(self.base_url%mobile,headers={'Host':'www.weibo.com',
# 					'Referer':'http://www.weibo.com/signup/mobile.php?lang=en-us&inviteCode=&from=&appsrc=&backurl=&showlogo=',
# 					'Upgrade-Insecure-Requests':'1','User-Agent':agent}
# 			 		, callback=self.parse_exist)
# 				proxy = random.choice(Config.PROXIES)
# 				print proxy
# 				print '-------------------------------------------------'
# 				print '-------------------------------------------------'
# 				req.meta['proxy'] = "http://%s" % proxy['ip_port']
# 				yield req
#
#
# 	def parse_exist(self, response):
# 		mobile = response.url[response.url.index('&value=')+7:]
# 		mobile = mobile[0: mobile.index('&')]
# 		re = json.loads(response.body_as_unicode())
# 		msg = re['data']['msg']
# 		code = re['code']
# 		exist = '0'
# 		if 'btn_login' in msg:
# 			exist = '1'
# 		print mobile + "\t" + exist + "\t" + code + "\t" + msg
# 		with codecs.open(self.outpath,'a','utf-8') as myfile:
# 			myfile.write("%s\t%s\t%s\n" % (mobile,exist,time.strftime("%Y-%m-%d")))
#
# 		if code == '600001' and 'btn_login' not in msg:
# 			print 'msg: %s' % msg
# 			print 'sleep 5 mins...'
# 			time.sleep(5*60)
